#!/bin/bash

PROTOBUF=$VCPKG_INSTALLED/arm64-osx/tools/protobuf/protoc
PROTOBUF_FILE=""
SUB_PROJECTS=()


function make_protobuf_file()
{
    echo "hello"
}

function make_cmakefile()
{
    # 移除旧cmake构建文件
    rm -rf CMakelists.txt

    TARGET_NAME=$1
    # 生成protobuf文件
    pb_files=`ls *.proto`
    rm -rf *.pb.cc
    for pb in ${pb_files[@]}
    do
        $PROTOBUF --proto_path=$PROTOBUF_FILE --cpp_out=$PROTOBUF_FILE $PWD/$pb
    done
    # 添加构建文件
    local src_files=(`ls *.cpp`)
    src_files+=(`ls *.cc`)
    if [ ${#src_files[*]} -gt 0 ];then
        SUB_PROJECTS=("${SUB_PROJECTS[@]}" $TARGET_NAME)
        echo "set(TARGET_NAME ${TARGET_NAME})" >> CMakelists.txt
        echo "set(CMAKE_CXX_STANDARD 20)" >> CMakelists.txt
        echo "set(SRC_LIST ${src_files[*]})" >> CMakelists.txt
        echo "set(VCPKG_INC \"\$ENV{VCPKG_INSTALLED}/arm64-osx\")" >> CMakelists.txt
        echo "include_directories(\${PROJECT_SOURCE_DIR}/thirdparty/include/ \${PROJECT_SOURCE_DIR}/src/ \${VCPKG_INC}/include)" >> CMakelists.txt
    fi
    # 添加子文件夹
    sub_dirs=(`ls`)
    for dir in ${sub_dirs[*]};
    do
        if [[ -d $dir && $dir != "main" ]]; then
            echo "add_subdirectory(${dir})" >> CMakelists.txt
            cd $dir
            make_cmakefile $dir
            cd ..
        fi
    done
    if [ ${#src_files[*]} -gt 0 ];then
        #echo -e $THIRD_TXT>> CMakelists.txt
        echo "add_library(\${TARGET_NAME} STATIC \${SRC_LIST})" >> CMakelists.txt
    fi
}

PROTOBUF_FILE=$PWD"/src"

cd src

make_cmakefile

echo "${SUB_PROJECTS[*]}"